package Engine.Movie
{
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;

	public class SwfPlay
	{
		private var _mcList:Array = new Array();		//MC列表
		private var _mainMC:MovieClip = null;			//主控MC
		private var _currPlayLabel:String = ""; 		//当前播放的动作
		private var _playIndex:int = 0;				//当前播放的动作位置
		private var _actionList:Array = new Array();	//播放的动作列表
		private var _backFun:Function = null;
		private var _arg:Object = null
		private var _booleanPre:Boolean = false;
			
		public function SwfPlay()
		{
			
		}
		
		public function insertMC(mc:MovieClip):void
		{
			this._mcList.push(mc);
			if(this._mainMC == null)
				this._mainMC = mc;
		}
		
		public function addHead(sp:Sprite):void
		{
			this._mainMC["pos_a"].addChild(sp);
		}
		
		//只播一次动作列表
		public function playOneTime(_labelList:Array,b:Boolean = false):void
		{
			if(_labelList.length == 0 || this._mainMC == null)
				return;
			this._booleanPre = b;
			this.destroyEvent();
			this._actionList = _labelList;
			this._playIndex = 0;
			this._mainMC.addEventListener(Event.ENTER_FRAME,onEnterFrameOneTime);
			this.playerLabel(this._playIndex);
			
		}
		
		//停止所有播放
		public function stopPlay():void
		{
			this.destroyEvent();
			this.setCallBackFun(null);
			for(var i:int=0;i<this._mcList.length;i++)
			{
				this._mcList[i].stop();
			}
		}
		
		//循环播放动作列表
		public function playEveryTime(_labelList:Array):void
		{
			if(_labelList.length == 0 || this._mainMC == null)
				return;
			this.destroyEvent();
			this._actionList = _labelList;
			this._playIndex = 0;
			this._mainMC.addEventListener(Event.ENTER_FRAME,onEnterFrameEveryTime);
			this.playerLabel(this._playIndex);
		}
		
		//设置播放回调
		public function setCallBackFun(fun:Function,arg:Object = null):void
		{
			this._backFun = fun;
			this._arg = arg;
		}
		
		private function destroyEvent():void
		{
			this._mainMC.removeEventListener(Event.ENTER_FRAME,onEnterFrameOneTime);
			this._mainMC.removeEventListener(Event.ENTER_FRAME,onEnterFrameEveryTime);
		}
		
		private function onEnterFrameOneTime(e:Event):void
		{
			if(this._mainMC.currentLabel != this._currPlayLabel)
			{
				this._playIndex++;
				if(this._playIndex == this._actionList.length)
				{
					if(this._booleanPre)
						this.stopPrePlay();
					else
						this.stopPlay();
					this._booleanPre = false;
					if(this._backFun != null)
					{
						var fun:Function = this._backFun;
						this._backFun = null
						fun(this._arg);
					}
				}
				else
				{
					this.playerLabel(this._playIndex);
				}
			}
			
		}
		
		private function stopPrePlay():void
		{
			this.destroyEvent();
			this.setCallBackFun(null);
			for(var i:int=0;i<this._mcList.length;i++)
			{
				this._mcList[i].prevFrame();
			}
		}
		
		private function playerLabel(index:int):void
		{
			this._currPlayLabel = this._actionList[index];
			for(var i:int=0;i<this._mcList.length;i++)
			{
				this._mcList[i].gotoAndPlay(this._currPlayLabel);
			}
		}
		
		private function onEnterFrameEveryTime(e:Event):void
		{
			if(this._mainMC.currentLabel != this._currPlayLabel)
			{
				this._playIndex++;
				if(this._playIndex == this._actionList.length)
				{
					this._playIndex = 0;
				}
				this.playerLabel(this._playIndex);
			}
			
		}
	}
}